const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs:[
      {title:'月光 脱毛'},
      {title:'纳米 脱毛'},
    ],
    crmItemList:[],
    crmItemIndex:0,
    countList:[],
    itemList:[],
    itemIndex:0,
    showConut:-1,
    btnTxt:{t:'请选择项目', type:0}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  onShow: function () {
    this.getData()
  },
  getData(){
    var t = this;
    wx.showLoading()
    app.request({
      url:'https://live.sgwxmini.com/beauty/rowNumber/itemDetailListByCustomerId',
      data:{
        hosId:app.api.siteinfo.hosId,
        customerId:wx.getStorageSync('customerId')
        // customerId:'15561060640014670'
      },
      success(e){
        e.data.itemList.forEach(i=>{
          i.title = i.name
          i.itemDetailList.forEach(l=>{
            l.count= e.data.countList[0];
            l.active = false;
          })
        })
        e.data.crmItemList.length > 0 && e.data.crmItemList.forEach(i=>{
          i.title = i.name
          i.buyItemList.forEach(l=>{
            l.active = false;
          })
        })

        t.setData({
          countList:e.data.countList,
          itemList:e.data.itemList,
          crmItemList:e.data.crmItemList,
          btnTxt:{t:'请选择项目', type:0}
        },wx.hideLoading)
      }
    })
  },
  setYuyueActive(e){
    var d = this.data;
    var item = d.crmItemList[d.crmItemIndex].buyItemList[e.currentTarget.dataset.idx];
    item.active = !item.active
    d.crmItemList.forEach((i,idx)=>{
      idx != d.crmItemIndex && i.buyItemList.forEach(i=>{
        i.active = false;
      })
    })

    this.setData({
      crmItemList:this.data.crmItemList,
      btnTxt:{t:'确认预约', type:1}
    })
    this.clearItemActive()
  },
  clearYuyueActive(){
    var d = this.data;
    d.crmItemList.forEach((i,idx)=>{
      i.buyItemList.forEach(i=>{
        i.active = false;
      })
    })
    this.setData({
      crmItemList:d.crmItemList
    })
  },
  setItemIndex(e){
    this.setData({
      itemIndex:e.currentTarget.dataset.idx
    })
  },
  setCrmItemIndex(e){
    console.log(e)
    this.setData({
      crmItemIndex:e.currentTarget.dataset.idx
    })
  },
  setShowConut(e){
    this.setData({
      showConut:e.currentTarget.dataset.idx
    })
  },
  setItemCount(e){
    var d = e.currentTarget.dataset;
    if(typeof d.idx == "undefined") return;
    this.data.itemList[this.data.itemIndex].itemDetailList[d.idx].count = d.count;
    this.setData({
      itemList:this.data.itemList
    },()=>{
      this.setPrice()
      this.hiddenCount()
    })

  },
  hiddenCount(){
    this.setData({
      showConut:-1
    })
  },
  setItemActive(e){
    var idx = e.currentTarget.dataset.idx,d = this.data;
    var item = d.itemList[d.itemIndex].itemDetailList[idx];
    item.active = !item.active;

    this.setData({
      itemList:d.itemList
    },this.setPrice)
    this.clearYuyueActive()
  },
  setPrice(){
    var d = this.data,price=0;
    d.itemList.forEach(i=>{
      i.itemDetailList.forEach(i=>{
        i.active && (price += i.count.count*i.one_price*100)
      })
    })

    this.setData({
      btnTxt:{t:`共${price/100}元，确认购买`, type:2}
    })

  },
  clearItemActive(){
    this.data.itemList.forEach(i=>{
      i.itemDetailList.forEach(l=>{
        l.active = false;
      })
    })
    this.setData({
      itemList:this.data.itemList
    })

  },
  submit(){
    var d = this.data,type = d.btnTxt.type;

    if(type == 0) return;
    if(type == 1){
      console.log('111')
      wx.navigateTo({
        url:'/pages/yuyue3/time/time',
        success(e) {
          e.eventChannel.emit('crmItemList',d.crmItemList)
        }
      })
    }
    if(type == 2){
      console.log('222')
      wx.navigateTo({
        url:'/pages/yuyue3/pay/pay',
        success(e) {
          e.eventChannel.emit('itemList',d.itemList)
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

})